<template>
  <div class="setting">
    <el-row>
      <el-button icon="el-icon-plus" @click="add">添加</el-button>
      <el-button icon="el-icon-delete" @click="dele">删除</el-button>
    </el-row>
    <el-table
      :data="tableData"
      border
      style="width: 100%"
      @selection-change="handleSelectionChange"
      :header-cell-style="{ 'text-align': 'center' }"
      :cell-style="{ 'text-align': 'center' }"
      :row-style="{ height: '48px' }"
      height="calc( 100vh - 200px)"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column fixed="left" label="操作" width="150">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleClick(scope.row)"
            >编辑</el-button
          >
          <el-button type="text" size="small" @click="handleClickq(scope.row)"
            >权限</el-button
          >
        </template>
      </el-table-column>
      <el-table-column fixed prop="Id" label="编号" width="150" />
      <el-table-column prop="RoleCode" label="编码" width="150" />
      <el-table-column prop="RoleName" label="角色" width="150" />
      <el-table-column prop="UsingFlag" label="使用标志" width="150" />
      <el-table-column prop="Note" label="备注" width="150" />
    </el-table>
    <el-dialog
      title="修改"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form :label-position="labelPosition" label-width="120px">
        <el-form-item label="编码">
          <el-input v-model="inputcode" placeholder="请输入编码" />
        </el-form-item>
        <el-form-item label="角色">
          <el-input v-model="inputname" placeholder="请输入角色" />
        </el-form-item>
        <el-form-item label="使用标志">
          <el-select v-model="inputsign" placeholder="请输入使用标志">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="inputremarks" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="clear">取 消</el-button>
        <el-button type="primary" @click="handleClose()">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="添加"
      :visible.sync="addVisible"
      width="30%"
      :before-close="handleaddClose"
    >
      <el-form :label-position="labelPosition" label-width="120px">
        <el-form-item label="编码">
          <el-input v-model="inputcode" placeholder="请输入编码" />
        </el-form-item>
        <el-form-item label="角色">
          <el-input v-model="inputname" placeholder="请输入角色" />
        </el-form-item>
        <!-- <el-form-item label="使用标志">
          <el-select v-model="inputsign" placeholder="请输入使用标志">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item> -->
        <el-form-item label="备注">
          <el-input v-model="inputremarks" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="clear">取 消</el-button>
        <el-button type="primary" @click="handleaddClose()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 权限 -->
    <el-dialog
      title="修改"
      :visible.sync="dialogVisibleq"
      width="30%"
      :before-close="handleCloseq"
    >
      <!-- 权限管理控制 -->
      <el-tree
        :data="data"
        show-checkbox
        node-key="id"
        ref="tree"
        :default-expanded-keys="[]"
        :default-checked-keys="ary"
        :props="defaultProps"
        @node-click="handleNodeClick"
      />
      <span slot="footer" class="dialog-footer">
        <el-button @click="clear">取 消</el-button>
        <el-button type="primary" @click="handleCloseq()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  API_GET_USER_LIST,
  API_USER_NEW,
  API_GET_XG,
  API_GET_SC,
  API_OPERATOR_GETCD,
  API_OPERATOR_SZJSQX,
  API_VILL_HQJSQX,
} from "../../api/system";
export default {
  name: "",
  data() {
    return {
      msg: "角色设置",
      labelPosition: "right",
      dialogVisible: false, // 修改模块显示隐藏
      addVisible: false, // 修改模块显示隐藏
      tableData: [],
      /*
       *
       */
      inputcode: "",
      inputname: "",
      inputsign: "",
      inputremarks: "",
      multipleSelection: [],
      options: [
        {
          value: "1",
          label: "使用",
        },
        {
          value: "2",
          label: "停用",
        },
      ],
      cardid: "",
      dialogVisibleq: false,
      /* 权限控制多选框 */
      data: [],
      defaultProps: {
        children: "children",
        label: "label",
      },
      ary: [],
      accountid: "",
    };
  },
  methods: {
    handleNodeClick(data) {
      console.log(data);
    },
    handleClickq(row) {
      let data = {
        RoleID: row.Id,
      };
      let arrr = [];
      API_VILL_HQJSQX(data)
        .then((res) => {
          console.log(res);
          this.accountid = row.Id;
          this.dialogVisibleq = true;
          res.ResList.forEach((item) => {
            // arrr.push(item.ModelName);
            if (item.PreModelID != 0) {
              arrr.push(item.ModelName);
            }
            // this.$message.success("获取权限成功！");
          });
          console.log(arrr);
          this.ary = arrr;
        })
        .catch((err) => {
          console.log(err);
          this.ary = [];
        });
      // console.log(row);
    },
    handleCloseq() {
      // 获取所有选中数据的集合  console.log(this.$refs.tree.getCheckedKeys().concat(this.$refs.tree.getHalfCheckedKeys()));
      this.$confirm("确认修改？")
        .then((_) => {
          let data = {
            lsname: this.$refs.tree
              .getCheckedKeys()
              .concat(this.$refs.tree.getHalfCheckedKeys())
              .toString(),
            roleid: this.accountid,
          };
          API_OPERATOR_SZJSQX(data)
            .then((res) => {
              console.log(res);
              this.$message.success("修改成功！");
              this.clear();
              this.submit();
            })
            .catch((err) => {
              console.log(err);
              this.clear();
            });
          // done();
          this.dialogVisibleq = false;
        })
        .catch((_) => {});
    },
    handleClick(row) {
      console.log(row);
      this.inputcode = row.RoleCode;
      this.inputname = row.RoleName;
      this.inputremarks = row.Note;
      this.inputsign = row.UsingFlag;
      this.cardid = row.Id;
      this.dialogVisible = true;
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleClose() {
      console.log(1);
      this.$confirm("确认修改？")
        .then((_) => {
          let data = {
            RoleCode: this.inputcode,
            RoleName: this.inputname,
            Note: this.inputremarks,
            UsingFlag: this.inputsign,
            Id: this.cardid,
          };
          API_GET_XG(data)
            .then((res) => {
              console.log(res);
              this.submit();
              this.$message.success("修改成功！");
              this.clear();
            })
            .catch((err) => {
              console.log(err);
              this.clear();
            });
          // done();
          this.dialogVisible = false;
        })
        .catch((_) => {
          this.clear();
        });
    },
    add() {
      this.addVisible = true;
      // API_USER_NEW;
    },
    dele() {
      let data = this.multipleSelection.map((item) => {
        return {
          Id: item.Id,
          RoleName: item.RoleName,
          Note: item.Note,
          RoleCode: item.RoleCode,
          UsingFlag: item.UsingFlag,
        };
      });
      if (data.length != 0) {
        API_GET_SC(data)
          .then((res) => {
            console.log(res);
            this.submit();
            this.$message.success("删除成功！");
          })
          .catch((err) => {
            console.log(err);
          });
      }
    },
    handleaddClose() {
      this.$confirm("确认添加？")
        .then((_) => {
          // done();
          let data = {
            RoleCode: this.inputcode,
            RoleName: this.inputname,
            Note: this.inputremarks,
            // UsingFlag: this.inputsign,
          };
          API_USER_NEW(data)
            .then((res) => {
              console.log(res);
              this.submit();
              this.clear();
              this.addVisible = false;
            })
            .catch((err) => {
              console.log(err);
              this.clear();
              this.addVisible = false;
            });
        })
        .catch((_) => {});
    },
    submit() {
      API_GET_USER_LIST()
        .then((res) => {
          console.log(res);
          this.tableData = res.ResList;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    clear() {
      this.inputcode = "";
      this.inputname = "";
      this.inputremarks = "";
      this.inputsign = "";
      this.cardid = "";
      this.ary = [];
      this.dialogVisible = false;
      this.dialogVisibleq = false;
      this.addVisible = false;
      this.getList();
    },
    getList() {
      let ary = [];
      API_OPERATOR_GETCD()
        .then((res) => {
          // console.log(res);
          res.ResList.forEach((item) => {
            if (item.PreModelID == 0) {
              item.children = [];
              ary.push({
                id: item.ModelName,
                label: item.ModelName,
                children: [],
                qqq: item.Id,
                ModelCode: item.ModelCode,
              });
            }
          });
          ary = ary.sort(this.compare("ModelCode"));
          console.log(ary);
          res.ResList.forEach((item) => {
            ary.forEach((i, index) => {
              if (i.qqq == item.PreModelID) {
                i.children.push({
                  id: item.ModelName,
                  label: item.ModelName,
                  ModelCode: item.ModelCode,
                  ModelID: item.ModelID,
                });
                // i.children = i.children.sort(this.compare("ModelCode"));
                // console.log(i.children);
              }
            });
          });
          ary.forEach((item) => {
            item.children = item.children.sort(this.compare("ModelCode"));
          });
          console.log(ary);
          this.data = ary;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 排序
    compare(property) {
      return function (a, b) {
        var value1 = a[property];
        var value2 = b[property];
        return value1 - value2;
      };
    },
  },
  created() {
    this.submit();
    this.getList();
  },
};
</script>

<style scoped lang="scss">
.setting {
  box-sizing: border-box;
  padding: 30px;
  max-width: 100%;
  .el-row {
    text-align: left;
  }

  .el-table {
    margin-top: 20px;
  }
}
</style>
